package cn.edu.sctu.sctu_lysd.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.my_view.RoundImageView;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.view.act.OrderDetailedAct;

/**
 * Created by wangfuxin on 2017-04-17.
 */

public class IndexListViewAda extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;

    private List<ParcelData> datas;
    private ViewHolder viewHolder;

    public IndexListViewAda(Context mContext, List<ParcelData> datas) {
        this.mContext = mContext;
        this.datas = datas;

        inflater = LayoutInflater.from(mContext);
    }

    public void refresh(List<ParcelData> datas){

        this.datas = (datas);

        this.notifyDataSetChanged();

    }

    public void loadMore(List<ParcelData> datas){

        this.datas.addAll(datas);

        this.notifyDataSetChanged();

    }

    @Override
    public int getCount() {

        if (datas != null) {
            return datas.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null || convertView.getTag() == null) {

            convertView = inflater.inflate(R.layout.item_my_orders, null);
            viewHolder = new ViewHolder();

          viewHolder.goodsNameTxt = (TextView) convertView.findViewById(R.id.item_goods_name_txt);
          viewHolder.goodsWeightTxt = (TextView) convertView.findViewById(R.id.item_goods_weight_txt);
          viewHolder.goodsDescribe = (TextView) convertView.findViewById(R.id.item_goods_description_txt);
          viewHolder.goodsImg = (ImageView) convertView.findViewById(R.id.item_goods_img);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.goodsNameTxt.setText(datas.get(position).getName());
        viewHolder.goodsWeightTxt.setText(datas.get(position).getWeight() + "kg");
        viewHolder.goodsDescribe.setText(datas.get(position).getDescription());

        Picasso.with(mContext)
                .load(datas.get(position).getParcelPhotoUrl())
                .into(viewHolder.goodsImg);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();

                bundle.putLong(OrderDetailedAct.INTENT_ID_TAG,datas.get(position).getId());
                bundle.putInt(OrderDetailedAct.INTENT_IS_MY_ORDER_TAG, OrderDetailedAct.NOT_MY_ORDER);

                JumpUtils.jumpActivity(mContext, OrderDetailedAct.class,bundle);
            }
        });


        return convertView;
    }


    static class ViewHolder {
         TextView goodsNameTxt, goodsWeightTxt, goodsDescribe;
         ImageView goodsImg;
    }
}
